package com.lenovo.mso2o;

import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.entity.AccessToken;
import com.lenovo.framework.entity.Res;
import com.lenovo.framework.rest.NetworkObserver;
import com.lenovo.framework.util.DLog;
import com.lenovo.mso2o.biz.AccountManagerImpl;
import com.lenovo.mso2o.entity.LogoutRequest;
import com.lenovo.mso2o.entity.TokenRequest;
import com.lenovo.mso2o.rest.AuthAPI;
import com.lenovo.mso2o.rest.ServiceGenerator;
import com.lenovo.mso2o.ui.LoginActivity;

import junit.framework.TestCase;

import org.jivesoftware.smack.util.Base64;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml")
public class ApplicationTest extends TestCase{
    LoginActivity app;

    @Before
    public void setup(){
        app = Robolectric.buildActivity(LoginActivity.class)
                .create().get();
        ContextManager.init(app);
    }

    @Test
    public void testLoginLogout(){
        final long start = System.currentTimeMillis();
        final AuthAPI authAPI = ServiceGenerator.createService(AuthAPI.class);
        Observable.range(0, 100)
                .observeOn(Schedulers.io())
                .map(new Func1<Integer, Object>() {
                    @Override
                    public Object call(final Integer integer) {
                        final TokenRequest req = new TokenRequest();
                        req.setAppversion("appversion");
                        req.setCid("cid");
                        req.setLoginname("mstest10@163.com");
                        req.setPassword(Base64.encodeBytes(Base64.encodeBytes("lenovoms".getBytes()).getBytes()));
                        req.setDeviceid("deviceid");
                        req.setOsversion("5.1");
                        authAPI.getAccessToken(req)
                                .subscribeOn(Schedulers.io())
                                .observeOn(Schedulers.io())
                                .unsubscribeOn(Schedulers.io())
                                .doOnTerminate(new Action0() {
                                    @Override
                                    public void call() {
                                        DLog.w("time:", "" + (System.currentTimeMillis() - start) / 100);
                                    }
                                })
                                .subscribe(new NetworkObserver<Res<AccessToken>>() {
                                    @Override
                                    public void onFailure(String tip) {
                                        DLog.w("fail:", integer + tip);
                                    }

                                    @Override
                                    public void onSuccess(Res<AccessToken> accessToken) {
                                        DLog.w("sucess login:", "" + integer);
                                        if (accessToken.getEcode() == 0) {
                                            AccountManagerImpl.instance.setToken(accessToken.getResult());
                                        } else {
                                            onFailure(accessToken.getReason());
                                        }
                                        LogoutRequest logoutRequest = new LogoutRequest();
                                        authAPI.logout(logoutRequest)
                                                .subscribeOn(Schedulers.io())
                                                .observeOn(Schedulers.computation())
                                                .unsubscribeOn(Schedulers.io())
                                                .doOnCompleted(new Action0() {
                                                    @Override
                                                    public void call() {

                                                    }
                                                }).toBlocking()
                                                .subscribe(new NetworkObserver<Res>() {
                                                    @Override
                                                    public void onSuccess(Res res) {
                                                        DLog.w("success logout:", "" + integer);
                                                    }

                                                    @Override
                                                    public void onFailure(String tip) {
                                                        DLog.w("fail logout:", "" + integer);
                                                    }
                                                });
                                    }
                                });
                        return null;
                    }
                });
    }
}